package com.first.nell.launch

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.first.nell.ProjectApplication
import com.first.nell.R
import com.first.nell.base.BaseViewModel
import com.first.nell.data.*
import com.first.nell.network.ApiUserReposity
import com.first.nell.utils.UserStateUtil

/** 用户流程viewModel */
class UserStateViewModel : BaseViewModel() {
    /** 用户渠道来源,默认为NO */
    var mFirstNellIsFirstNellAdFirstNellSource = false

    /** 权限列表 */
    var mFirstNellPermissionFirstNellList = mutableListOf<DataPermission>()

    /** 查询app配置回调 */
    private val mFirstNellFetchFirstNellSysFirstNellConfigFirstNellLiveData =
        MutableLiveData<ReqBase>()

    /** 查询app版本回调 */
    private val mFirstNellFetchFirstNellAppFirstNellVersionFirstNellLiveData =
        MutableLiveData<ReqBase>()

    /** 查询登录otp回调 */
    private val mFirstNellFetchFirstNellOtpFirstNellLiveData = MutableLiveData<ReqDoLogin>()

    /** 登录回调 */
    private val mFirstNellDoFirstNellLoginFirstNellLiveData = MutableLiveData<ReqDoLogin>()

    /** 查询用户状态回调 */
    private val mFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData =
        MutableLiveData<ReqBase>()

    /** 查询用户状态回调-点下一步按键时调用 */
    private val mFirstNellFetchFirstNellUserFirstNellStateFirstNellForFirstNellCommitFirstNellActionFirstNellLiveData =
        MutableLiveData<ReqBase>()

    /** 查询app配置回调 */
    val onFirstNellFetchFirstNellSysFirstNellConfigFirstNellLiveData =
        Transformations.switchMap(mFirstNellFetchFirstNellSysFirstNellConfigFirstNellLiveData) { query ->
            ApiUserReposity.fetchFirstNellAppFirstNellConfig(this, query)
        }

    /** 查询app版本回调 */
    val onFirstNellFetchFirstNellAppFirstNellVersionFirstNellLiveData =
        Transformations.switchMap(mFirstNellFetchFirstNellAppFirstNellVersionFirstNellLiveData) { query ->
            ApiUserReposity.fetchFirstNellAppFirstNellVersion(this, query)
        }

    /** 初始化审核账号回调 */
    val onFirstNellInitFirstNellAccFirstNellUserFirstNellLiveData = MutableLiveData<Boolean>()

    /** 查询登录otp回调 */
    val onFirstNellFetchFirstNellOtpFirstNellLiveData =
        Transformations.switchMap(mFirstNellFetchFirstNellOtpFirstNellLiveData) { query ->
            ApiUserReposity.fetchFirstNellOtp(this, query)
        }

    /** 登录回调 */
    val onFirstNellDoFirstNellLoginFirstNellLiveData =
        Transformations.switchMap(mFirstNellDoFirstNellLoginFirstNellLiveData) { query ->
            ApiUserReposity.doFirstNellLogin(this, query)
        }

    /** 查询用户状态回调 */
    val onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData =
        Transformations.switchMap(mFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData) { query ->
            ApiUserReposity.fetchFirstNellUserFirstNellState(this, query)
        }

    /** 查询用户状态回调-点下一步按键时调用 */
    val onFirstNellFetchFirstNellUserFirstNellStateFirstNellForFirstNellCommitFirstNellActionFirstNellLiveData =
        Transformations.switchMap(
            mFirstNellFetchFirstNellUserFirstNellStateFirstNellForFirstNellCommitFirstNellActionFirstNellLiveData
        ) { query ->
            ApiUserReposity.fetchFirstNellUserFirstNellState(this, query)
        }

    /** 查询app配置 */
    fun fetchFirstNellSysFirstNellConfig() {
        mFirstNellFetchFirstNellSysFirstNellConfigFirstNellLiveData.value = ReqBase()
    }

    /** 查询app版本 */
    fun fetchFirstNellAppFirstNellVersion() {
        mFirstNellFetchFirstNellAppFirstNellVersionFirstNellLiveData.value = ReqBase()
    }

    /** 查询登录otp */
    fun fetchFirstNellOtp(reqDoLogin: ReqDoLogin) {
        mFirstNellFetchFirstNellOtpFirstNellLiveData.value = reqDoLogin
    }

    /** 登录 */
    fun doFirstNellLogin(reqDoLogin: ReqDoLogin) {
        mFirstNellDoFirstNellLoginFirstNellLiveData.value = reqDoLogin
    }

    /** 查询用户状态 */
    fun fetchFirstNellUserFirstNellState(reqBase: ReqBase = ReqBase()) {
        mFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.value = reqBase
    }

    /** 查询用户状态-点下一步按键时调用 */
    fun fetchFirstNellUserFirstNellStateFirstNellForFirstNellCommitFirstNellAction(reqBase: ReqBase) {
        mFirstNellFetchFirstNellUserFirstNellStateFirstNellForFirstNellCommitFirstNellActionFirstNellLiveData.value =
            reqBase
    }

    /** 初始化权限列表 */
    fun initFirstNellPermissionFirstNellList() {
        mFirstNellPermissionFirstNellList.clear()
        mFirstNellPermissionFirstNellList.add(
            DataPermission(
                imgRes = R.drawable.first_nell_permission_phone,
                title = ProjectApplication.context.getString(R.string.first_nell_permission_phone),
                detail = ProjectApplication.context.getString(R.string.first_nell_permission_phone_des)
            )
        )
        mFirstNellPermissionFirstNellList.add(
            DataPermission(
                imgRes = R.drawable.first_nell_permission_sms,
                title = ProjectApplication.context.getString(R.string.first_nell_permission_sms),
                detail = ProjectApplication.context.getString(R.string.first_nell_permission_sms_des)
            )
        )

        //最后添加data safty
        mFirstNellPermissionFirstNellList.add(
            DataPermission(
                imgRes = R.drawable.first_nell_permission_calendar,
                title = ProjectApplication.context.getString(R.string.first_nell_permission_data_security),
                detail = ProjectApplication.context.getString(R.string.first_nell_permission_data_security_des)
            )
        )
    }

    /** 初始化审核账号 */
    fun initFirstNellAccFirstNellUser(b4dmtbepk5hgxvt9jxryn3a: DataReviewAccount) {
        if (b4dmtbepk5hgxvt9jxryn3a.accTk.isBlank()) {
            onFirstNellInitFirstNellAccFirstNellUserFirstNellLiveData.value = false
        } else {
            //更新登录状态
            UserStateUtil.updateFirstNellLoginFirstNellState(
                RevObjDoLogin(
                    token = b4dmtbepk5hgxvt9jxryn3a.accTk,
                    userId = b4dmtbepk5hgxvt9jxryn3a.accTk,
                    mobile = b4dmtbepk5hgxvt9jxryn3a.accM,
                    type = "OLD",
                    directLoginFlag = "NO"
                )
            )
            onFirstNellInitFirstNellAccFirstNellUserFirstNellLiveData.value = true
        }
    }
}